package com.whq.thrift.api.web.admin.controller.bs;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.consumelog.ConsumeLog;
import com.whq.thrift.api.web.admin.service.consumelog.ConsumeLogService;
import com.whq.thrift.api.web.admin.util.DateFormatUtil;

@Controller
@Scope("prototype")
public class ConsumeLogStatController {
	@Autowired
	ConsumeLogService consumeLogService;
	
	@RequestMapping(value="bs/consumelogstat/statByApiCode")
	public String statByApiCode(
	        @RequestParam(value="time", required=false) String time,
	        @RequestParam(value="start_time", required=false) String start_time,
	        @RequestParam(value="end_time", required=false) String end_time,
	        @RequestParam(value="api", required=false) String api,
	        @RequestParam(value="dir",required=false) Integer dir,
	        @RequestParam(value="result", required=false) Integer result,
			Model model) {
		ConsumeLog consumeLog = new ConsumeLog();
		
		// 查询参数
	    Map<String, Object> whereParams = new HashMap<String, Object>(0);
	    Date timeDt = (time != null && StringUtil.isNotBlank(time)) ? DateFormatUtil.formatToDate(time) :  new Date();
	    String timeDate = DateFormatUtil.dateToString(timeDt);
	    
	    consumeLog.setTime(timeDt);
	    whereParams.put("tableSeq", consumeLog.getTableSeq());
	    
	    if (StringUtil.isNotBlank(start_time)) {
	    	whereParams.put("start_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + start_time + ":00"));
	    } else {
	    	SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
	    	start_time = sdfHour.format(new Date()) +  ":00";
	    	whereParams.put("start_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + start_time + ":00"));
	    }
	    
	    if (StringUtil.isNotBlank(end_time)) {
	    	whereParams.put("end_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + end_time + ":00"));
	    } else {
	    	SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");
	    	end_time = sdfTime.format(new Date());
	    	whereParams.put("end_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + end_time + ":00"));
	    }
	    
	    if (api != null) {
	    	whereParams.put("api", api);
	    }
	    
	    if(dir!=null){
	    	whereParams.put("dir", dir);
	    }
	    
	    if (result != null) {
	    	whereParams.put("result", result);
	    }
	    
	    List<ConsumeLog> dataList = consumeLogService.statByApiCode(whereParams);
	    
	    model.addAttribute("time", time);
	    model.addAttribute("start_time", start_time);
	    model.addAttribute("end_time", end_time);
	    model.addAttribute("api", api);
	    model.addAttribute("dir",dir);
	    model.addAttribute("result", result);
		model.addAttribute("dataList", dataList);
		return "bs/consumelogstat/statByApiCode";
	}
	
	@RequestMapping(value="bs/consumelogstat/statByCode")
	public String statByCode(
	        @RequestParam(value="time", required=false) String time,
	        @RequestParam(value="start_time", required=false) String start_time,
	        @RequestParam(value="end_time", required=false) String end_time,
	        @RequestParam(value="dir" ,required=false) Integer dir,
	        @RequestParam(value="result", required=false) Integer result,
			Model model) {
		ConsumeLog consumeLog = new ConsumeLog();
		
		// 查询参数
	    Map<String, Object> whereParams = new HashMap<String, Object>(0);
	    Date timeDt = (time != null && StringUtil.isNotBlank(time)) ? DateFormatUtil.formatToDate(time) :  new Date();
	    String timeDate = DateFormatUtil.dateToString(timeDt);
	    
	    consumeLog.setTime(timeDt);
	    whereParams.put("tableSeq", consumeLog.getTableSeq());
	    
	    if (StringUtil.isNotBlank(start_time)) {
	    	whereParams.put("start_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + start_time + ":00"));
	    } else {
	    	SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
	    	start_time = sdfHour.format(new Date()) +  ":00";
	    	whereParams.put("start_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + start_time + ":00"));
	    }
	    
	    if (StringUtil.isNotBlank(end_time)) {
	    	whereParams.put("end_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + end_time + ":00"));
	    } else {
	    	SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");
	    	end_time = sdfTime.format(new Date());
	    	whereParams.put("end_time", DateFormatUtil.formatStringToFullDate(timeDate + " " + end_time + ":00"));
	    }
	    
	    if(dir!=null){
	    	whereParams.put("dir", dir);
	    }
	    
	    if (result != null) {
	    	whereParams.put("result", result);
	    }
	    
	    List<ConsumeLog> dataList = consumeLogService.statByCode(whereParams);
	    
	    model.addAttribute("time", time);
	    model.addAttribute("start_time", start_time);
	    model.addAttribute("end_time", end_time);
	    model.addAttribute("result", result);
	    model.addAttribute("dir",dir);
		model.addAttribute("dataList", dataList);
		return "bs/consumelogstat/statByCode";
	}

}
